//
//  MLCompose.swift
//  MLSina20160624
//
//  Created by manmanlang on 16/7/3.
//  Copyright © 2016年 manmanlang. All rights reserved.
//

import UIKit

class MLCompose: UITextView {
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        /* Proerty'self.xxx' not initialized at super.init call 属性类型必须是可选值 */
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(placeholderLabel)
        placeholderLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(8)
            make.leading.equalTo(self).offset(5)
            make.width.equalTo(self).offset(5 * 2)
        }
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MLCompose.TextViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
    }
    func TextViewTextDidChange() {
        placeholderLabel.hidden = self.hasText()
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //占位文字 属性必须是可选值
    var placeholder: String? {
        didSet {
            placeholderLabel.text = placeholder
        }
    }
    
    //重写font属性 通过didSet方法监听font的改变
    override var font:UIFont? {
        didSet {
            placeholderLabel.font = font
        }
    }
    
    private lazy var placeholderLabel:UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.grayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        return label
        
    }()


}
